package com.slm.krstartimes.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;

import com.slm.krstartimes.R;
import com.slm.krstartimes.adapter.CollectAdapter;
import com.slm.krstartimes.db.sqlite.SQLiteDataBaseManager;
import com.slm.krstartimes.utils.L;
import com.slm.krstartimes.utils.News;
import com.slm.krstartimes.utils.SlideCutListView;

import java.util.List;


/**
 * Created by dllo on 15/8/15.
 */
public class CollectActivity extends Activity implements SlideCutListView.RemoveListener, GestureDetector.OnGestureListener, View.OnTouchListener {
    private SlideCutListView collectListView;
    private CollectAdapter collectAdapter;
    private List<News> list;
    private GestureDetector mGestureDetector;
    private LinearLayout collect_layout;
    private int verticalMinDistance = 50;
    private int middleDistance = 80;
    private int minVelocity = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.collect_layout);

        initView();

        list = MainActivity.sqLiteDataBaseTools.query(SQLiteDataBaseManager.LIKE);
        collectAdapter = new CollectAdapter(CollectActivity.this, list);
        collectListView.setAdapter(collectAdapter);
        collectListView.setRemoveListener(this);
        collectListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            /**
             * 列表监听跳转
             * @param adapterView
             * @param view
             * @param i
             * @param l
             */
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent newsDetail = new Intent(CollectActivity.this, NewsDetailActivity.class);// 新建intent对象,跳转本身
                newsDetail.putExtra("newsId", list.get(i).getNewsid());//向目标传递数据
                newsDetail.putExtra("title", list.get(i).getTitle());
                newsDetail.putExtra("postdate", list.get(i).getCommentcount());
                L.i("点击了" + i);
                startActivity(newsDetail);// 开始跳转
            }
        });
    }

    /**
     * 初始化
     */
    private void initView() {
        collectListView = (SlideCutListView) findViewById(R.id.collectListView);
        collect_layout = (LinearLayout) findViewById(R.id.collect_layout);
        collect_layout.setOnTouchListener(this);
        collect_layout.setLongClickable(true);
        mGestureDetector = new GestureDetector(this);
    }

    /**
     * 删除新闻
     *
     * @param direction
     * @param position
     */
    @Override
    public void removeItem(SlideCutListView.RemoveDirection direction, int position) {
        MainActivity.sqLiteDataBaseTools.deleteById(SQLiteDataBaseManager.LIKE, list.get(position).getNewsid());
        list.remove(position);
        collectAdapter.notifyDataSetChanged();
        L.i("删除了第" + position + "数据");
    }

    @Override
    public boolean onDown(MotionEvent motionEvent) {
        return false;
    }

    @Override
    public void onShowPress(MotionEvent motionEvent) {

    }

    @Override
    public boolean onSingleTapUp(MotionEvent motionEvent) {
        return false;
    }

    @Override
    public boolean onScroll(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) {
        return false;
    }

    @Override
    public void onLongPress(MotionEvent motionEvent) {

    }

    /**
     * 滑动返回
     *
     * @param e1
     * @param e2
     * @param v
     * @param v1
     * @return
     */
    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float v, float v1) {
        if (e2.getX() - e1.getX() > middleDistance && Math.abs(v) > minVelocity && (Math.abs(e2.getY() - e1.getY()) < verticalMinDistance)) {
            L.i("滑动返回," + e2 + "," + e1);
            finish();
        }
        return false;
    }

    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
        return false;
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        mGestureDetector.onTouchEvent(ev);
        // scroll.onTouchEvent(ev);
        return super.dispatchTouchEvent(ev);
    }
}
